package gen.tools.deploy;

import gen.tools.Widgets;
import gen.tools.structures.S2;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryMemphisDepMessage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QueryMemphisDepMessage() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		ArrayList<S2> meses = new ArrayList<S2>();
		
		final String sql ="SELECT fileName,errMsg FROM depmemphisdetail WHERE depMemphisSN=?";
		try {
			//wid.getLatestDepMemphisDetailId(depMemphisSN)
			
			conn = wid.getConn();
			ps = conn.prepareStatement(sql);
			//ps.setInt(1, depMemphisSN);
			rs = ps.executeQuery();
			while ( rs.next() ) {
				S2 mes = new S2();
				mes.setS1(rs.getString("fileName"));
				mes.setS2(rs.getString("errMsg"));
				meses.add(mes);
			}
			
		} catch( Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if ( rs !=null ) { rs.close(); }
				if ( ps !=null ) { ps.close(); }
				if ( conn !=null ) { conn.close(); }
			} catch (Exception e) { e.printStackTrace(); }
		}
		
		response.getWriter().write( new Gson().toJson(meses) );
	}

}
